import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_demo/Constants.dart';
import 'home_bloc/cubit.dart';




class HomeSearchPage extends StatefulWidget {

  ///搜素内容
  List<String> contents = [
    "盘点申请",
    "预盘单",
    "差异处理",
    "库存盘点单",
    "下载数据",
    "快速收银",
    "点餐助手",
    "销售流水",
    "交班",
    "首页",
    "参数设置",
  ];

  @override
  _HomeSearchPageState createState() => _HomeSearchPageState();
}

class _HomeSearchPageState extends State<HomeSearchPage> with SingleTickerProviderStateMixin {

  ///逻辑处理
  final cubit = HomeBlocCubit();

  ///搜素框
  String searchValue = "";

  ///搜素框Controller
  TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => cubit,
      child: Scaffold(
          body: Container(
        color: Colors.white,
        child: Column(
          children: [
            Container(
              color: Colors.white,
              child: Row(
                children: [
                  Expanded(
                    child: Center(
                      child: Container(
                        child: Padding(
                          padding: const EdgeInsets.fromLTRB(15, 20, 0, 15),
                          child: SizedBox(
                            height: 40,
                            child: Container(
                              child: TextField(
                                // controller: TextEditingController.fromValue(TextEditingValue(
                                //   // 设置内容
                                //     text: searchValue,
                                //     // 保持光标在最后
                                //     selection: TextSelection.fromPosition(TextPosition(
                                //         affinity: TextAffinity.downstream,
                                //         offset: searchValue.length)))) ,
                                autofocus: true,
                                style: TextStyle(textBaseline: TextBaseline.alphabetic),
                                controller: _controller,
                                scrollPadding: EdgeInsets.all(100),
                                keyboardType: TextInputType.text,
                                inputFormatters: <TextInputFormatter>[
                                  LengthLimitingTextInputFormatter(24) //限制长度
                                ],
                                decoration: InputDecoration(
                                  ///文本居中 设置边框
                                  contentPadding: EdgeInsets.only(top: 0, bottom: 0),
                                  suffixIcon: IconButton(
                                      icon: Icon(
                                        Icons.highlight_off,
                                        size: 20,
                                        color: Constants.hexStringToColor("#7A73C7"),
                                      ),
                                      onPressed: () {
                                        setState(() {
                                          searchValue = "";
                                        });
                                        _controller.text = searchValue;
                                      }),
                                  prefixIcon: Icon(
                                    Icons.search_outlined,
                                    color: Constants.hexStringToColor("#7A73C7"),
                                  ),
                                  fillColor: Color(0x30cccccc),
                                  filled: true,
                                  enabledBorder: OutlineInputBorder(borderSide: BorderSide(color: Color(0x00FF0000)), borderRadius: BorderRadius.all(Radius.circular(80))),
                                  hintText: '',
                                  focusedBorder: OutlineInputBorder(borderSide: BorderSide(color: Color(0x00000000)), borderRadius: BorderRadius.all(Radius.circular(80))),
                                ),
                                onChanged: (inputValue) {
                                  setState(() {
                                    searchValue = inputValue;
                                  });
                                },
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(0, 0, 10, 0),
                    child: InkWell(
                      onTap: () => Navigator.pop(context),
                      child: Container(
                        width: 60,
                        height: 30,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(5),
                          color: Colors.white,
                        ),
                        child: Center(child: Text("取消", style: TextStyle(fontSize: 16))),
                      ),
                    ),
                  )
                ],
              ),
            ),
            searchValue == "" ? Container() : getSearchResultWidget(),
          ],
        ),
      )),
    );
  }

  ///搜索结果
  Widget getSearchResultWidget() {
    List<String> results = [];
    List.generate(widget.contents.length, (index) {
      if (widget.contents[index].contains(searchValue)) {
        results.add(widget.contents[index]);
      }
    });
    return SingleChildScrollView(
      child: Column(
        children: List.generate(results.length, (index) {
          return InkWell(
              onTap: () {
                _controller.text = results[index];
              },
              child: Container(
                height: 50,
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.all(8),
                decoration: BoxDecoration(
                    color: Colors.white,
                    border: Border(
                        // bottom: BorderSide(color: Colors.black12),
                        )),
                child: Row(
                  children: [
                    Padding(
                      padding: const EdgeInsets.fromLTRB(8, 8, 8, 8),
                      child: Icon(Icons.add_photo_alternate_outlined),
                    ),
                    Text(results[index]),
                  ],
                ),
              ));
        }),
      ),
    );
  }
}
